import 'package:casdon/app_providers.dart';
import 'package:casdon/core/network/api_client_provider.dart';

void startSSEAIRecipe({
  required String text,
  required void Function(String data) onMessage,
  required void Function(Exception e) onError,
  required void Function() onDone,
  required void Function() onConnectSuccess,
}) async {
  final dio = getProvider(apiClientProvider);
  dio.sse(
    'http://dev.casdon.com.cn:30888/api/client-product/sse',
    headers: {
      'Accept': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive',
    },
    query: {"recipeName": text},
    onMessage: (message) {
      print('SSE Received message: $message');
      onMessage(message);
    },
    onError: (error) {
      print('SSE Error occurred: $error');
      onError(error);
    },
    onDone: () {
      print('SSE stream closed.');
      onDone();
    },
    onConnectSuccess: () {
      print('SSE stream start.');
      onConnectSuccess();
    },
  );
}
